<?php
/**
 * 商品管理控制器
 * @author 孙银川
 * @date   2017.12.25
 */

namespace app\admin\controller;
use app\common\model\Goods as GoodsModel;
use app\common\model\DataExport;

class Goods extends Base{

	/**
     * 商品列表(已审核)
     */
	public function goodsList(){
		$gmodel = new GoodsModel();
		// 店铺ID
		$shop_id = input('param.shop_id');
		if($shop_id) $map['gd_shop_id'] = ['g.gd_shop_id','=',$shop_id];

		// 搜索
		if((input('get.act')=='s') && ($name=input('post.name'))){
			// 查询条件
			$map['gd_name'] = ['g.gd_name','like','%'.$name.'%'];
			$data = $gmodel->enquiries(10,$map);
		}
		$map['gd_audit'] = ['g.gd_audit','>',0];
		// 获取所有已审核商品
		$data = $gmodel->enquiries(10,$map);
		$goods_list = $data->items();
		foreach($goods_list as $k=>$v){
			// 商品状态
			if($v['gd_status'] == 1){
				$goods_list[$k]['gd_status'] = '上架';
			}else{
				$goods_list[$k]['gd_status'] = '下架';
			}
			// 审核状态
			if($v['gd_audit'] == 1){
				$goods_list[$k]['gd_audit']='通过';
			}else{
				$goods_list[$k]['gd_audit']='未通过';
			}
			// 上架时间
			$goods_list[$k]['gd_sale_time'] = date('Y-m-d H:i:s',$v['gd_sale_time']);
			// 添加时间
			$goods_list[$k]['gd_create_time'] = date('Y-m-d H:i:s',$v['gd_create_time']);
		}

		// 导出excel表格
		if(input('get.act') == 'ep'){
			$dmodel = new DataExport();
			// 标题行
        	$title = array('商品ID','商品名称','商品分类','商品价格','商品状态','审核状态','店铺名称','上架时间','添加时间');
			$res = $dmodel->export($goods_list,$title,'商品数据表格');
		}
		
		$this->assign('page',$data->page);
		$this->assign('count',$data->count);
		$this->assign('goods_list',$goods_list);
		return $this->fetch('goods/goodsList');
	}

	/**
     * 商品待审核列表
     */
	public function goodsAudit(){
		$gmodel = new GoodsModel();
		$map['gd_audit'] = ['g.gd_audit','=',0];
		$data = $gmodel->enquiries(10,$map);
		$goods_list = $data->items();
		foreach($goods_list as $k=>$v){
			$goods_list[$k]['gd_audit'] = '待审核';
			// 添加时间
			$goods_list[$k]['gd_create_time'] = date('Y-m-d H:i:s',$v['gd_create_time']);
			unset($goods_list[$k]['gd_status']);
			unset($goods_list[$k]['gd_sale_time']);
		}

		// 导出excel表格
		if(input('get.act') == 'ep'){
			$dmodel = new DataExport();
			// 标题行
        	$title = array('商品ID','商品名称','商品分类','商品价格','审核状态','店铺名称','添加时间');
			$res = $dmodel->export($goods_list,$title,'待审核商品数据表格');
		}

		$this->assign('page',$data->page);
		$this->assign('count',$data->count);
		$this->assign('goods_list',$goods_list);
		return $this->fetch('goods/goodsAudit');
	}

	/**
     * 商品详情
     */
	public function goodsDetail(){
		$id = input('get.id');
		// 获取商户信息
		$gmodel = new GoodsModel();
		if($id) $goods = $gmodel->detail($id);

		$this->assign('goods',$goods);
		return $this->fetch('goods/goodsDetail');
	}

	/**
     * ajax商品上架/下架
     */
	public function goodsAjax(){
		$gmodel = new GoodsModel();
		if(input('get.id')){
			$data['gd_id'] = input('get.id');
			$data['gd_status'] = input('get.status');
			if($data['gd_status'] == 1) $data['gd_sale_time'] = time();
			$res = $gmodel->edit($data);
			return $res;
		}
	}

	/**
     * ajax商品审核
     */
	public function auditAjax(){
		$data = input('post.');
		if($data['ids']){
			$ids = $data['ids'];
			unset($data['ids']);
			// 审核时间
			$data['gd_audit_time'] = time();
			$res = db('goods')->where('gd_id in('.$ids.')')->update($data);
			return $res;
		}
	}
}
?>